package com.yl.chainStore.util.generator

import com.baomidou.mybatisplus.annotation.DbType
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.baomidou.mybatisplus.core.toolkit.StringPool
import com.baomidou.mybatisplus.generator.AutoGenerator
import com.baomidou.mybatisplus.generator.InjectionConfig
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.po.TableInfo
import com.baomidou.mybatisplus.generator.config.rules.DateType
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine

class GeneratorFactory {

    fun generator() {
        println("begin generating")
        val projectPath = System.getProperty("user.dir")
        var mpg = AutoGenerator()
        //全局配置
        var globalConfig = GlobalConfig()
        globalConfig.isKotlin = true //关键点使用kotlin
        globalConfig.author = "cxw" //作者
        globalConfig.isOpen = false
//        globalConfig.outputDir = System.getProperty("user.dir") + "/src/main/kotlin" //创建地址
        globalConfig.outputDir =  "D:/src/main/kotlin" //创建地址

        globalConfig.isFileOverride = true
        globalConfig.idType = IdType.AUTO
        globalConfig.xmlName = "%sMapper"
        globalConfig.mapperName = "%sMapper"
//        globalConfig.serviceName="%sService"
//        globalConfig.serviceImplName = "%sServiceImpl"
        globalConfig.controllerName = "%sController"
        globalConfig.isBaseColumnList = true
        globalConfig.isBaseResultMap = true
        globalConfig.dateType = DateType.ONLY_DATE
        mpg.globalConfig = globalConfig
        var dsc = DataSourceConfig()
        dsc.dbType = DbType.POSTGRE_SQL
        dsc.driverName = "org.postgresql.Driver"

//        dsc.url = "jdbc:postgresql://116.62.176.145:1027/cxw_test"
//        dsc.username = "postgres"
//        dsc.password = "!QAZ@WSX3edc"
//        dsc.schemaName = "public"

        dsc.url = "jdbc:postgresql://127.0.0.1:5432/hhkq_db"
        dsc.username = "postgres"
        dsc.password = "chen1993"
        dsc.schemaName = "public"

        mpg.dataSource = dsc
        // 包配置
        var pc = PackageConfig();
//            pc.setModuleName(scanner("模块名"));
        pc.parent = "com.yl.chainStore"
        mpg.packageInfo = pc
        // 自定义配置
        val cfg: InjectionConfig = object : InjectionConfig() {
            override fun initMap() {
                // to do nothing
            }
        }
        val focList: MutableList<FileOutConfig> = ArrayList()
        focList.add(object : FileOutConfig("/templates/mapper.xml.ftl") {
            override fun outputFile(tableInfo: TableInfo): String? {
                // 自定义输入文件名称
                return (projectPath.toString() + "/src/main/resources/mapper/"
                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML)
            }
        })
        //　config xml　template and outpath
        cfg.fileOutConfigList = focList
        mpg.cfg = cfg
        //模板配置
        val tc = TemplateConfig()
        tc.entityKt = "/temp/vm/entity.kt"
        tc.mapper = "/temp/vm/mapper.java"
        tc.controller = "/temp/vm/controller.java"
        mpg.template = tc.setService(null).setController(null).setServiceImpl(null)
        val strategy = StrategyConfig()
        strategy.naming = NamingStrategy.underline_to_camel
        strategy.columnNaming = NamingStrategy.underline_to_camel
        strategy.isRestControllerStyle = true
        strategy.isControllerMappingHyphenStyle = true
        val sum = StringBuffer()
        sum.append(pc.moduleName).append("_")
        strategy.setTablePrefix(sum.toString())
        strategy.setInclude(
            "pay_item_price_db",
//            "sick_customer_access_log_plan","sick_customer_access_log_plan_detail"
//            ,"pay_customer_balance_in_log","pay_customer_coupons"
//            ,"pay_customer_integral_flow","pay_customer_money_flow",
//            "pay_user_pay_callback_log"
        )

        strategy.superMapperClass = "com.baomidou.mybatisplus.core.mapper.BaseMapper"

        mpg.strategy = strategy
        mpg.templateEngine = FreemarkerTemplateEngine()
        mpg.execute();
    }

}

fun main(args: Array<String>) {
    GeneratorFactory().generator()
}
